package com.hp.airmap.common;


import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 网络请求工具类
 * Retrofit
 */
public class RetrofitUtil {

    public static final String SERVER_URL = "http://jsonplaceholder.typicode.com/";

    private static WebService service;
    public static WebService getWebService() {
        if (service == null) {
            service = createApi(SERVER_URL, WebService.class);
        }
        return service;
    }

    public static <T> T createApi(String url, Class<T> clazz) {
        return getRetrofit(url).create(clazz);
    }

    private static Retrofit getRetrofit(String url) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                //打印请求Log
                .addInterceptor
                        (new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit;
    }



}
